﻿using System;
using System.ComponentModel;
using System.Configuration;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using Hardcodet.Wpf.TaskbarNotification;
using hygge_imaotai.Domain;
using imaotai.Domain;
using MaterialDesignThemes.Wpf;
using NLog;
using NLog.Config;
using NLog.Targets.Wrappers;



namespace hygge_imaotai.UserInterface.Window;

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow {

    /// <summary>
    /// 
    /// </summary>
    private static readonly Logger log = LogManager.GetCurrentClassLogger();

    public MainWindow() {
        InitializeComponent();
        Task.Factory.StartNew(() => Thread.Sleep(2500)).ContinueWith(t => {
            //note you can use the message queue from any thread, but just for the demo here we 
            //need to get the message queue from the snackbar, so need to be on the dispatcher
            MainSnackbar.MessageQueue?.Enqueue("欢迎使用 i茅台预约小助手");
        }, TaskScheduler.FromCurrentSynchronizationContext());
        var dataContextCopy = new MainWindowViewModel(MainSnackbar.MessageQueue!);
        dataContextCopy.SelectedItem = dataContextCopy.DemoItems[0];
        DataContext = dataContextCopy;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void OnCopy(object sender, ExecutedRoutedEventArgs e) {
        if (e.Parameter is string stringValue) {
            try {
                Clipboard.SetDataObject(stringValue);
            } catch (Exception ex) {
                log.Error(ex);
            }
        }
    }

    private void MainWindow_OnLoaded(object sender, RoutedEventArgs e) {
        log.Info("系统开始启动");

        NavRail.Visibility = Visibility.Collapsed;
        NavDrawer.OpenMode = DrawerHostOpenMode.Standard;
        NavDrawer.IsLeftDrawerOpen = true;
        MenuToggleButton.Visibility = Visibility.Collapsed;
        //FAB.Visibility = Visibility.Collapsed;
        //DrawerFAB.Visibility = Visibility.Visible;

        tbIcon.ShowBalloonTip("提示", "欢迎使用自动抢茅台工具，系统将在每天指定时间自动帮您预约抢购。请及时关注短信结果。", BalloonIcon.Info);
    }

    private void Window_SizeChanged(object sender, SizeChangedEventArgs e) {
        ChangeShowBySize();
    }

    private void MainWindow_OnStateChanged(object? sender, EventArgs e) {
        if (this.WindowState == WindowState.Minimized) {
            this.Visibility = Visibility.Hidden;
            tbIcon.ShowBalloonTip("提示","我已最小化到右下角，点击托盘小图标再次显示。", BalloonIcon.Info);
        }
    }

    private void ChangeShowBySize() {
        if (ActualWidth <= 700) {
            NavRail.Visibility = Visibility.Collapsed;
            NavDrawer.OpenMode = DrawerHostOpenMode.Modal;
            NavDrawer.IsLeftDrawerOpen = false;
            MenuToggleButton.Visibility = Visibility.Visible;
            //FAB.Visibility = Visibility.Visible;
            //DrawerFAB.Visibility = Visibility.Collapsed;
        } else if (ActualWidth > 700 && ActualWidth <= 1600) {
            NavRail.Visibility = Visibility.Visible;
            NavDrawer.OpenMode = DrawerHostOpenMode.Modal;
            NavDrawer.IsLeftDrawerOpen = false;
            MenuToggleButton.Visibility = Visibility.Visible;
            //FAB.Visibility = Visibility.Collapsed;
            //DrawerFAB.Visibility = Visibility.Collapsed;
        } else if (ActualWidth > 1600) {
            NavRail.Visibility = Visibility.Collapsed;
            NavDrawer.OpenMode = DrawerHostOpenMode.Standard;
            NavDrawer.IsLeftDrawerOpen = true;
            MenuToggleButton.Visibility = Visibility.Collapsed;
            //FAB.Visibility = Visibility.Collapsed;
            //DrawerFAB.Visibility = Visibility.Visible;
        }
    }

    private void GitHubButton_OnClick(object sender, RoutedEventArgs e) => Link.OpenInBrowser(ConfigurationManager.AppSettings["GitHub"]);

    private void MenuOpen_Click(object sender, RoutedEventArgs e) {
        NavDrawer.IsLeftDrawerOpen = false;
        if (!(ActualWidth > 1600)) return;
        NavRail.Visibility = Visibility.Visible;
        MenuToggleButton.Visibility = Visibility.Visible;
    }

    private void UIElement_OnPreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) {
        if (NavDrawer.OpenMode is DrawerHostOpenMode.Standard) return;
        //until we had a StaysOpen flag to Drawer, this will help with scroll bars
        var dependencyObject = Mouse.Captured as DependencyObject;

        while (dependencyObject != null) {
            if (dependencyObject is ScrollBar) return;
            dependencyObject = VisualTreeHelper.GetParent(dependencyObject);
        }

        MenuToggleButton.IsChecked = false;
    }

    private void MenuToggleButton_OnClick(object sender, RoutedEventArgs e) {
        DemoItemsSearchBox.Focus();
        if (!(ActualWidth > 1600)) return;
        NavRail.Visibility = Visibility.Collapsed;
        MenuToggleButton.Visibility = Visibility.Collapsed;
    }

    private void MenuDarkModeButton_Click(object sender, RoutedEventArgs e)
        => ModifyTheme(DarkModeToggleButton.IsChecked == true);

    private static void ModifyTheme(bool isDarkTheme) {
        var paletteHelper = new PaletteHelper();
        var theme = paletteHelper.GetTheme();

        theme.SetBaseTheme(isDarkTheme ? Theme.Dark : Theme.Light);
        paletteHelper.SetTheme(theme);
    }

    private async void MenuPopupButton_OnClick(object sender, RoutedEventArgs e) {
        var sampleMessageDialog = new SampleMessageDialog {
            Message = { Text = ((ButtonBase)sender).Content.ToString() }
        };

        await DialogHost.Show(sampleMessageDialog, "RootDialog");
    }

    private void OnSelectedItemChanged(object sender, DependencyPropertyChangedEventArgs e)
        => MainScrollViewer.ScrollToHome();

    private void TaskbarIcon_OnTrayLeftMouseUp(object sender, RoutedEventArgs e) {
        Visibility = this.Visibility == Visibility.Hidden ? Visibility.Visible : Visibility.Hidden;
    }

    private void MainWindow_OnClosing(object? sender, CancelEventArgs e)
    {
        // 阻止窗口关闭  
        e.Cancel = true;

        // 最小化窗口  
        WindowState = WindowState.Minimized;
    }
}